from .view import SourceView, CollectorTaskView
from . import schema
from typing import List
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission


router_source = APIRouter(prefix='/source', tags=['数据源'])
router_meta = {'module': '数据采集', 'sub_module': '数据源'}


@router_source.get('/', response_model=schema.PageSources, openapi_extra=router_meta, name='source_list_get',
                 summary='数据源列表')
def source_list_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['source:read'])):
    return SourceView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_source.post('/', response_model=schema.SourceOut, openapi_extra=router_meta, name='source_post', summary='数据源创建')
def source_post(body: schema.CreateSource,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['source:write'])):
    return SourceView.create_source(session, body=body.dict())


@router_source.get('/{id}', response_model=schema.SourceOut, openapi_extra=router_meta, name='source_get',
                 summary='数据源详情')
def source_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['source:read'])):
    return SourceView.get_item(session, filter_=[SourceView.id == id])


@router_source.patch('/{id}', response_model=schema.SourceOut, openapi_extra=router_meta, name='source_patch',
                   summary='数据源修改')
def source_patch(id: str,
               body: schema.EditSource,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['source:write'])):
    return SourceView.update_item(session, filter_=[SourceView.id == id], update=body.dict(exclude_unset=True))


@router_source.delete('/', openapi_extra=router_meta, name='source_delete', summary='数据源删除')
def source_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['source:write'])):
    return {'count': SourceView.delete_sources(session, filter_=[SourceView.id.in_(body.ids)])}


# 数据源采集任务
@router_source.post('/{source_id}/collector', response_model=schema.CollectorOut, openapi_extra=router_meta, name='collector_post', summary='采集任务创建')
def collector_post(source_id: str,
                   body: schema.CreateCollector,
                   session=Depends(db.get_session),
                   user=Security(permission.security, scopes=['source:write'])):
    return CollectorTaskView.create_collector(session, body={**body.dict(), 'source_id': source_id})


@router_source.get('/{source_id}/collector', response_model=List[schema.CollectorOut], openapi_extra=router_meta, name='collector_get',
                 summary='采集任务列表')
def collector_get(source_id: str,
                  session=Depends(db.get_session),
                  user=Security(permission.security, scopes=['source:read'])):
    return CollectorTaskView.get_items(session, filter_=[CollectorTaskView.source_id == source_id]).all()


@router_source.patch('/{source_id}/collector/{id}', response_model=schema.CollectorOut, openapi_extra=router_meta, name='collector_patch',
                   summary='采集任务修改')
def collector_patch(source_id: str, 
                    id: str,
                    body: schema.EditCollector,
                    session=Depends(db.get_session),
                    user=Security(permission.security, scopes=['source:write'])):
    return CollectorTaskView.update_collector(session, filter_=[CollectorTaskView.source_id == source_id, CollectorTaskView.id == id], 
                                                   update=body.dict(exclude_unset=True))


@router_source.post('/collector/verify', openapi_extra=router_meta, name='collector_post', summary='采集任务配置验证')
def collector_post(body: schema.VerifyCollector,
                   user=Security(permission.security, scopes=['source:read'])):
    return CollectorTaskView.execute(**body.dict(), verify=True)


@router_source.delete('/{source_id}/collector', openapi_extra=router_meta, name='collector_delete', summary='采集任务删除')
def collector_delete(source_id: str,
                     body: schema.BatchId,
                     session=Depends(db.get_session),
                     user=Security(permission.security, scopes=['source:write'])):
    return {'count': CollectorTaskView.delete_collectors(session, filter_=[CollectorTaskView.source_id == source_id, CollectorTaskView.id.in_(body.ids)])}


@router_source.post('/{source_id}/collector/{id}/enable', response_model=schema.EnableCollector, openapi_extra=router_meta, name='collector_enable',
                   summary='采集任务启用与禁用')
def collector_enable(source_id: str, 
                    id: str,
                    body: schema.EnableCollector,
                    session=Depends(db.get_session),
                    user=Security(permission.security, scopes=['source:write'])):
    return CollectorTaskView.collector_enable(session, filter_=[CollectorTaskView.source_id == source_id, CollectorTaskView.id == id], 
                                              update=body.dict(exclude_unset=True))


# 数据源绑定解析规则组
@router_source.post('/source/{id}/group', openapi_extra=router_meta, name='source_parser_group_bind',
                  summary='数据源关联解析规则组')
def source_parser_group_bind(id: str,
                    body: schema.BatchId,
                    session=Depends(db.get_session),
                    user=Security(permission.security, scopes=['source:write'])):
    return SourceView.bind_source_parser_groups(session, id, body.ids)


@router_source.delete('/source/{id}/group', openapi_extra=router_meta, name='source_parser_group_delete',
                    summary='数据源解绑解析规则组')
def source_parser_group_delete(id: str,
                      body: schema.BatchId,
                      session=Depends(db.get_session),
                      user=Security(permission.security, scopes=['source:write'])):
    return SourceView.delete_source_parser_groups(session, id, body.ids)
